﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyBongDa.HeThongLuuTru;
using QuanLyBongDa.HeThongXuLy;
using QuanLyBongDa.HeThongGiaoDien;

namespace QuanLyBongDa.HeThongGiaoDien
{
    public partial class FormBangXepHang : Form
    {
        BangXepHangDTO xhHienTai = new BangXepHangDTO();

        public FormBangXepHang()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            BangXepHangDTO xh = new BangXepHangDTO();
            xh.Ngay = txtNgay.Text;
            xh.TenDoiBong = txtTenDoiBong.Text;
            xh.Thang = int.Parse(txtThang.Text);
            xh.Hoa = int.Parse(txtHoa.Text);
            xh.Thua = int.Parse(txtThua.Text);
            xh.HieuSo = float.Parse(txtHieuSo.Text);
            xh.Hang = int.Parse(txtHang.Text);

            if (BangXepHang.XepHang(xh))
            {
                MessageBox.Show("Thêm Xếp Hạng Thành Công ^^", "Thông Báo");
                HienThiBangXepHang();
            }
            else
            {
                MessageBox.Show("Thêm Xếp Hạng Thất Bại @@", "Thông Báo");
            }
        }
        void HienThiBangXepHang()
        {
            DataTable ds = BangXepHang.BangXepHangDoiBong();
            dgvBangXepHang.DataSource = ds;

        }

        private void dgvBangXepHang_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvBangXepHang.SelectedCells.Count > 0)
            {
                // lay chi so cua dong dang duoc chon
                int row = dgvBangXepHang.SelectedCells[0].RowIndex;
                // lay doi bong dc chon tu DGV ra
                DongToiBangXepHangHienTai(dgvBangXepHang.Rows[row]);
                // hien thi doi bong len form:

                txtNgay.Text = xhHienTai.Ngay;
                //txtMaSanNha.Text = dbHienTai.MaSanNha.ToString();
                txtTenDoiBong.Text = xhHienTai.TenDoiBong;
                txtThang.Text = xhHienTai.Thang.ToString();
                txtHoa.Text = xhHienTai.Hoa.ToString();
                txtThua.Text = xhHienTai.Thua.ToString();
                txtHieuSo.Text = xhHienTai.HieuSo.ToString();
                txtHang.Text = xhHienTai.Hang.ToString();

            }
        }
        private void DongToiBangXepHangHienTai(DataGridViewRow row)
        {
            // Cap nhat thong tin cho doi tuong xhHienTai (DTO)
            xhHienTai.SoThuTu = (int)row.Cells[0].Value;
            xhHienTai.Ngay = (String)row.Cells[1].Value;
            xhHienTai.TenDoiBong = (String)row.Cells[2].Value;
            xhHienTai.Thang = (int)row.Cells[3].Value;
            xhHienTai.Hoa = (int)row.Cells[4].Value;
            xhHienTai.Thua = (int)row.Cells[5].Value;
            xhHienTai.HieuSo = (float)row.Cells[6].Value;
            xhHienTai.Hang = (int)row.Cells[7].Value;
        }
        

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (BangXepHang.XoaXepHang(xhHienTai))
            {
                MessageBox.Show("Xóa Xếp Hạng Thành Công ^_^", "Thông Báo");
                HienThiBangXepHang();
            }
            else
            {
                MessageBox.Show("Xóa Xếp Hạng Thất Bại @@", "Thông Báo");
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            xhHienTai.Ngay = txtNgay.Text;
            xhHienTai.TenDoiBong = txtTenDoiBong.Text;
            xhHienTai.Thang = int.Parse(txtThang.Text);
            xhHienTai.Hoa = int.Parse(txtHoa.Text);
            xhHienTai.Thua = int.Parse(txtThua.Text);
            xhHienTai.HieuSo = float.Parse(txtHieuSo.Text);
            xhHienTai.Hang = int.Parse(txtHang.Text);
            if (BangXepHang.SuaBangXepHang(xhHienTai))
            {
                MessageBox.Show("Sửa Bảng Xếp Hạng Thành Công ^_^", "Thông Báo");
                HienThiBangXepHang();
            }
            else
            {
                MessageBox.Show("Sửa Bảng Xếp Hạng Thất Bại @@", "Thông Báo");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvBangXepHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void FormBangXepHang_Load(object sender, EventArgs e)
        {
            HienThiBangXepHang();
        }
    }
}
